﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Biz_Dal.Biz;

namespace Biz_Dal.Dal
{
    public class DalDonDatHang
    {
        //lấy id mới
        internal static int IdMaxDDH()
        {
            DalCommon dal = new DalCommon();
            int intIdt = dal.BookIdeas_ShopDataContext.DonDatHangs.Max(u => u.idDDH);
            return intIdt;
        }
        internal static int Insert(BizDonDatHang bizDonDatHang)
        {
            DalCommon dal = new DalCommon();
            int result = bizDonDatHang.Id;
            DonDatHang ddh = new DonDatHang();
            ddh.idUser = bizDonDatHang.KhachHang.idUser;
            ddh.ngayDat = bizDonDatHang.NgayDat;
            ddh.ngayGiao = bizDonDatHang.NgayGiao;
            ddh.idTTDH = bizDonDatHang.IdTrangThai;
            ddh.tongTien = bizDonDatHang.TongTien;

            dal.BookIdeas_ShopDataContext.DonDatHangs.InsertOnSubmit(ddh);
            dal.BookIdeas_ShopDataContext.SubmitChanges();

            result = ddh.idDDH;
            return result;
        }

        internal static List<BizDonDatHang> getDDHByIDKhachHang(int idKH)
        {
            DalCommon dal = new DalCommon();
            var querry = (from kh in dal.BookIdeas_ShopDataContext.DonDatHangs
                          where kh.idUser == idKH
                          select new BizDonDatHang
                          {
                              Id=kh.idDDH,
                              NgayDat=kh.ngayDat,
                              NgayGiao=kh.ngayGiao,
                              TongTien=(double)kh.tongTien,
                              IdTrangThai=kh.idTTDH,
                              IdKH=kh.idUser,
                          }).Distinct<BizDonDatHang>();
            if (querry.Count() == 0)
            {
                return null;
            }
            else
            {
                return querry.ToList<BizDonDatHang>();
            }
        }
    }
}
